<template>
    <div class="card content-box" :style="{ minHeight: clientHeight + 'px' }">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="140px">
            <el-form-item label="活动名称:" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入活动名称"/>
            </el-form-item>
            <el-form-item label="活动电话:" prop="phone">
                <el-input v-model="ruleForm.phone" placeholder="请输入活动电话" />
            </el-form-item>
            <el-form-item label="活动区:" prop="region">
                <el-select v-model="ruleForm.region" placeholder="请选择活动区">
                    <el-option label="Zone one" value="shanghai" />
                    <el-option label="Zone two" value="beijing" />
                </el-select>
            </el-form-item>
            <el-form-item label="活动时间:" required>
                <el-form-item prop="date1">
                    <el-date-picker v-model="ruleForm.date1" type="date" placeholder="选择一个日期" style="width: 100%" />
                </el-form-item>
                <el-col class="text-center" :span="1">
                    <span class="text-gray-500">-</span>
                </el-col>
                <el-form-item prop="date2">
                    <el-time-picker v-model="ruleForm.date2" placeholder="选择一个时间" style="width: 100%" />
                </el-form-item>
            </el-form-item>
            <el-form-item label="即时交货:" prop="delivery">
                <el-switch v-model="ruleForm.delivery" />
            </el-form-item>
            <el-form-item label="资源:" prop="resource">
                <el-radio-group v-model="ruleForm.resource">
                    <el-radio label="赞助" />
                    <el-radio label="场地" />
                </el-radio-group>
            </el-form-item>
            <el-form-item label="活动形式:" prop="desc">
                <el-input v-model="ruleForm.desc" type="textarea" placeholder="请输入活动形式"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">创建</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts" name="dynamicForm">
import { reactive, ref } from "vue";
import { checkPhoneNumber } from "@/utils/eleValidate";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
    name: "",
    phone: "",
    region: "",
    date1: "",
    date2: "",
    delivery: false,
    resource: "",
    desc: ""
});

const rules = reactive<FormRules>({
    name: [
        { required: true, message: "请输入活动名称", trigger: "blur" },
        { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" }
    ],
    phone: [{ required: true, validator: checkPhoneNumber, trigger: "blur" }],
    region: [
        {
            required: true,
            message: "请选择活动场地",
            trigger: "change"
        }
    ],
    date1: [
        {
            type: "date",
            required: true,
            message: "请选择一个日期",
            trigger: "change"
        }
    ],
    date2: [
        {
            type: "date",
            required: true,
            message: "请选择一个时间",
            trigger: "change"
        }
    ],
    resource: [
        {
            required: true,
            message: "请选择活动资源",
            trigger: "change"
        }
    ],
    desc: [{ required: true, message: "请输入活动形式", trigger: "blur" }]
});

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            ElMessage.success("提交的数据为 : " + JSON.stringify(ruleForm));
        } else {
            console.log("error submit!", fields);
        }
    });
};

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
};
const clientHeight = document.body.clientHeight - 106
</script>

<style scoped lang="scss">
.content-box{
    width: 100%;
}
.el-form {
  width: 100%;
  .text-center {
    text-align: center;
  }
}

</style>
